package com.shanghaionstar.carmanage.pay.alipay;

import com.onstar.cn.common.util.CommonUtil;
import com.shanghaionstar.R;
import com.tencent.mm.opensdk.modelbase.BaseResp;

import java.util.HashMap;

/**
 * 
* @ClassName: AlipayResult 
* @Description: AlipayResult
* @author Xiaofeng Feng
* @date 2015年9月9日 下午3:02:19
 */
public class AlipayResult {
	private HashMap<String, Integer> resultMap = new HashMap<String, Integer>();
	private static AlipayResult alipayResult;

	public static AlipayResult getInstance() {
		if (alipayResult == null) {
			alipayResult = new AlipayResult();
		}
		return alipayResult;
	}

	private AlipayResult() {
		resultMap.put("9000", R.string.alipay_9000);
		resultMap.put("8000", R.string.alipay_8000);
		resultMap.put("4000", R.string.SB027_MSG010);
		resultMap.put("6001", R.string.SB027_MSG011);
		resultMap.put("6002", R.string.alipay_6002);
		resultMap.put("1", R.string.order_status_1);
		resultMap.put("2", R.string.order_status_2);
		resultMap.put("3", R.string.order_status_3);

		resultMap.put(BaseResp.ErrCode.ERR_OK + "", R.string.alipay_9000);
		resultMap.put(BaseResp.ErrCode.ERR_COMM + "", R.string.SB027_MSG010);
		resultMap.put(BaseResp.ErrCode.ERR_USER_CANCEL + "", R.string.SB027_MSG011);
		resultMap.put(BaseResp.ErrCode.ERR_UNSUPPORT + "", R.string.wechat_version_error);
		resultMap.put(BaseResp.ErrCode.ERR_AUTH_DENIED + "", R.string.SB027_MSG010);
		resultMap.put(BaseResp.ErrCode.ERR_SENT_FAILED + "", R.string.SB027_MSG010);
	}

	public Integer findResultByAlipayStatus(String alipayStatus) {
		try {
			Integer result = resultMap.get(alipayStatus);
			if (CommonUtil.isNull(result)) {
				return R.string.alipay_9999;
			} else {
				return result;
			}
		} catch (Exception e) {
			return R.string.alipay_9999;
		}
	}

	private String resultStatus;
	private String result;
	private String memo;

	public void getResult(String rawResult) {
		try {
			String[] resultParams = rawResult.split(";");
			for (String resultParam : resultParams) {
				if (resultParam.startsWith("resultStatus")) {
					resultStatus = gatValue(resultParam, "resultStatus");
				}
				if (resultParam.startsWith("result")) {
					result = gatValue(resultParam, "result");
				}
				if (resultParam.startsWith("memo")) {
					memo = gatValue(resultParam, "memo");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private String gatValue(String content, String key) {
		String prefix = key + "={";
		return content.substring(content.indexOf(prefix) + prefix.length(), content.lastIndexOf("}"));
	}

	public String getResultStatus() {
		return resultStatus;
	}

	public void setResultStatus(String resultStatus) {
		this.resultStatus = resultStatus;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}
}
